package com.yzy.loader
{
	import com.yzy.loader.event.LoadInfoEvent;
	import com.yzy.loader.vo.loadItemVo;
	import com.yzy.models.SetLibs;
	import com.yzy.tool.ByteResolve;
	import com.yzy.tool.ReadFile;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	
	public class LoaderList extends EventDispatcher
	{
		public function LoaderList()
		{
		}
		/**
		 * 
		 * 
		 */		
		public  function GetLoaderList(urlList:String):void{
			DataLoader.loaderData(urlList,function completefun(data:String):void{
				var loadItemVec:Vector.<loadItemVo>=ReadFile.strToVector(data,loadItemVo,Vector.<loadItemVo>) as Vector.<loadItemVo>;
				SetDataLib(loadItemVec);
			},function progressfun(bytesTotal:Number,bytesLoaded:Number):void{
				var loadinfoEvt:LoadInfoEvent=new LoadInfoEvent(LoadInfoEvent.PROGRESS_EVENT);
				loadinfoEvt.bytesLoaded=bytesLoaded;
				loadinfoEvt.bytesTotal=bytesTotal;
				disPatchEvent(loadinfoEvt);
			});
		}
		
		//*****************************************************
		// loader list 
		//*****************************************************
		/**
		 * 
		 * @param loadItemVec
		 */		
		public  function SetDataLib(loadItemVec:Vector.<loadItemVo>,isResolve:Boolean=false):void{
			var loadItem:loadItemVo=loadItemVec.pop();
			DataLoader.loaderData(loadItem.url,function listLoadComplete(data:ByteArray):void{
				SetLibs.setLibsData(loadItem.name,data);
				//	
				var next:Function = function fun():void{
						if(loadItemVec.length){
							SetDataLib(loadItemVec,isResolve)
						}
						else{
							disPatchEvent(new LoadInfoEvent(LoadInfoEvent.COMPLETE_EVENT));
						}
				}
				//
				if(isResolve){
					ByteResolve.byteForSwf(SetLibs.getLibsData(loadItem.name),loadItem.name,loadItem.type,next)
				}else{
					next();
				};
				
			},function listLoadProgress(bytesTotal:Number,bytesLoaded:Number):void{
				var loadinfoEvt:LoadInfoEvent=new LoadInfoEvent(LoadInfoEvent.PROGRESS_EVENT);
				loadinfoEvt.bytesLoaded=bytesLoaded;
				loadinfoEvt.bytesTotal=bytesTotal;
				disPatchEvent(loadinfoEvt);
			});  
		}

		
		//*****************************************************
		// dispatchEvent
		//*****************************************************
		private function disPatchEvent(evt:Event):void{
			this.dispatchEvent(evt);
		}
		
		//*****************************************************
		// Singleton 
		//*****************************************************
		private static var _instant:LoaderList;
		public static function get instant():LoaderList
		{
			if(null == _instant)
			{
				_instant =  new LoaderList();
			}
			return _instant;
		}
	}
}